home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1935 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.4 KB  |  62 lines

  1. Newsgroups: comp.lang.c++,comp.lang.c
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: Performance: C vs. C++
  5. Message-ID: <marnoldDL5z3s.24I@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <30F6BAAC.12B5@iastate.edu> <4da9pn$a45@news.bridge.net>
  8. Date: Sun, 14 Jan 1996 09:16:40 GMT
  9. Sender: marnold@netcom6.netcom.com
  10.  
  11. David Byrden <100101.2547@compuserve.com> writes:
  12.  
  13.  
  14. >Steve;
  15.  
  16. >C and C++ are so similar that I would expect similar code to produce 
  17. >almost identical performance. However, note that while virtual functions 
  18. >cause overhead when you use them, exceptions in most present compilers can 
  19. >cause overhead *at every function call*.
  20.  
  21. Well...
  22.  
  23. Borland C++ only generates the required exception overhead if the function
  24. contains exception constructs (try, throw, etc.).  A "normal" function that
  25. does not contain such constructs has no extra code. 
  26.  
  27. For example, with C++ exceptions fully enabled (and with other functions
  28. in the same translation unit clearly getting extra exception code---via
  29. examination of C++-to-assembly output), a simple function like this...
  30.  
  31.    void Nothing()
  32.       {
  33.       }
  34.  
  35. ...generates a single instruction (a ret).  Granted, such a function could
  36. be recognized and treated as a special case by the compiler, but my 
  37. experience with Borland C++ in general proves otherwise.  With Borland C++, 
  38. you do not pay for C++ exceptions at every function call.  And, I'd actually 
  39. be surprised if many other compilers are less efficient than this and 
  40. require some kind of exception overhead in every single function.
  41.  
  42.  
  43. And on your other point...
  44.  
  45. By the way, virtual functions are usually implemented as a call-though-
  46. function-pointer.  To get the same functionality offered by C++ virtual
  47. functions in C, you'd probably use the same approach (a function pointer
  48. whose address is set to call the appropriate version of a function).  In 
  49. this sense, C++ is no less efficient than C.  
  50.  
  51.  
  52. Regards,
  53. -------------------------------------------------------------------------
  54. Matt Arnold                       |        | ||| | |||| |  | | || ||
  55. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  56. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  57. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  58. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  59. -------------------------------------------------------------------------
  60.  
  61.  
  62.